home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / Date / Human.php < prev    next >
Encoding:
PHP Script  |  2005-12-02  |  7.8 KB  |  210 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. //
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2005 Allan Kent                                   |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to the New BSD license, That is bundled  |
  8. // | with this package in the file LICENSE, and is available through      |
  9. // | the world-wide-web at                                                |
  10. // | http://www.opensource.org/licenses/bsd-license.php                   |
  11. // | If you did not receive a copy of the new BSDlicense and are unable   |
  12. // | to obtain it through the world-wide-web, please send a note to       |
  13. // | pear-dev@lists.php.net so we can mail you a copy immediately.        |
  14. // +----------------------------------------------------------------------+
  15. // | Author: Allan Kent <allan@lodestone.co.za>                           |
  16. // +----------------------------------------------------------------------+
  17.  
  18. /**
  19.  * Class to convert date strings between Gregorian and Human calendar formats
  20.  *
  21.  * The Human Calendar format has been proposed by Scott Flansburg and can be
  22.  * explained as follows:
  23.  *  The year is made up of 13 months
  24.  *  Each month has 28 days
  25.  *  Counting of months starts from 0 (zero) so the months will run from 0 to 12
  26.  *  New Years day (00) is a monthless day
  27.  *  Note: Leap Years are not yet accounted for in the Human Calendar system
  28.  *
  29.  * PHP versions 4 and 5
  30.  *
  31.  * @category   Date and Time
  32.  * @package    Date
  33.  * @author     Allan Kent <allan@lodestone.co.za>
  34.  * @copyright  1997-2005 Allan Kent
  35.  * @license    http://www.opensource.org/licenses/bsd-license.php  New BSD License
  36.  * @version    CVS: $Id: Human.php,v 1.5 2005/11/15 00:16:40 pajoye Exp $
  37.  * @link       http://pear.php.net/package/Date
  38.  * @since      File available since Release 1.3
  39.  */
  40.  
  41. /**
  42.  * Class to convert date strings between Gregorian and Human calendar formats
  43.  *
  44.  * The Human Calendar format has been proposed by Scott Flansburg and can be
  45.  * explained as follows:
  46.  *  The year is made up of 13 months
  47.  *  Each month has 28 days
  48.  *  Counting of months starts from 0 (zero) so the months will run from 0 to 12
  49.  *  New Years day (00) is a monthless day
  50.  *  Note: Leap Years are not yet accounted for in the Human Calendar system
  51.  *
  52.  * @author     Allan Kent <allan@lodestone.co.za>
  53.  * @copyright  1997-2005 Allan Kent
  54.  * @license    http://www.opensource.org/licenses/bsd-license.php  New BSD License
  55.  * @version    Release: 1.4.6
  56.  * @link       http://pear.php.net/package/Date
  57.  * @since      Class available since Release 1.3
  58.  */
  59. class Date_Human
  60. {
  61.     /**
  62.      * Returns an associative array containing the converted date information
  63.      * in 'Human Calendar' format.
  64.      *
  65.      * @param int day in DD format, default current local day
  66.      * @param int month in MM format, default current local month
  67.      * @param int year in CCYY format, default to current local year
  68.      *
  69.      * @access public
  70.      *
  71.      * @return associative array(
  72.      *               hdom,       // Human Day Of Month, starting at 1
  73.      *               hdow,       // Human Day Of Week, starting at 1
  74.      *               hwom,       // Human Week of Month, starting at 1
  75.      *               hwoy,       // Human Week of Year, starting at 1
  76.      *               hmoy,       // Human Month of Year, starting at 0
  77.      *               )
  78.      *
  79.      * If the day is New Years Day, the function will return
  80.      * "hdom" =>  0
  81.      * "hdow" =>  0
  82.      * "hwom" =>  0
  83.      * "hwoy" =>  0
  84.      * "hmoy" => -1
  85.      *  Since 0 is a valid month number under the Human Calendar, I have left
  86.      *  the month as -1 for New Years Day.
  87.      */
  88.     function gregorianToHuman($day=0, $month=0, $year=0)
  89.     {
  90.         /*
  91.          * Check to see if any of the arguments are empty
  92.          * If they are then populate the $dateinfo array
  93.          * Then check to see which arguments are empty and fill
  94.          * those with the current date info
  95.          */
  96.         if ((empty($day) || (empty($month)) || empty($year))) {
  97.             $dateinfo = getdate(time());
  98.         }
  99.         if (empty($day)) {
  100.             $day = $dateinfo["mday"];
  101.         }
  102.         if (empty($month)) {
  103.             $month = $dateinfo["mon"];
  104.         }
  105.         if (empty($year)) {
  106.             $year = $dateinfo["year"];
  107.         }
  108.         /*
  109.          * We need to know how many days into the year we are
  110.          */
  111.         $dateinfo = getdate(mktime(0, 0, 0, $month, $day, $year));
  112.         $dayofyear = $dateinfo["yday"];
  113.         /*
  114.          * Human Calendar starts at 0 for months and the first day of the year
  115.          * is designated 00, so we need to start our day of the year at 0 for
  116.          * these calculations.
  117.          * Also, the day of the month is calculated with a modulus of 28.
  118.          * Because a day is 28 days, the last day of the month would have a
  119.          * remainder of 0 and not 28 as it should be.  Decrementing $dayofyear
  120.          * gets around this.
  121.          */
  122.         $dayofyear--;
  123.         /*
  124.          * 28 days in a month...
  125.          */
  126.         $humanMonthOfYear = floor($dayofyear / 28);
  127.         /*
  128.          * If we are in the first month then the day of the month is $dayofyear
  129.          * else we need to find the modulus of 28.
  130.          */
  131.         if ($humanMonthOfYear == 0) {
  132.             $humanDayOfMonth = $dayofyear;
  133.         } else {
  134.             $humanDayOfMonth = ($dayofyear) % 28;
  135.         }
  136.         /*
  137.          * Day of the week is modulus 7
  138.          */
  139.         $humanDayOfWeek = $dayofyear % 7;
  140.         /*
  141.          * We can now increment $dayofyear back to it's correct value for
  142.          * the remainder of the calculations
  143.          */
  144.         $dayofyear++;
  145.         /*
  146.          * $humanDayOfMonth needs to be incremented now - recall that we fudged
  147.          * it a bit by decrementing $dayofyear earlier
  148.          * Same goes for $humanDayOfWeek
  149.          */
  150.         $humanDayOfMonth++;
  151.         $humanDayOfWeek++;
  152.         /*
  153.          * Week of the month is day of the month divided by 7, rounded up
  154.          * Same for week of the year, but use $dayofyear instead $humanDayOfMonth
  155.          */
  156.         $humanWeekOfMonth = ceil($humanDayOfMonth / 7);
  157.         $humanWeekOfYear = ceil($dayofyear / 7);
  158.         /*
  159.          * Return an associative array of the values
  160.          */
  161.         return array(
  162.                      "hdom" => $humanDayOfMonth,
  163.                      "hdow" => $humanDayOfWeek,
  164.                      "hwom" => $humanWeekOfMonth,
  165.                      "hwoy" => $humanWeekOfYear,
  166.                      "hmoy" => $humanMonthOfYear );
  167.     }
  168.  
  169.     /**
  170.      * Returns unix timestamp for a given Human Calendar date
  171.      *
  172.      * @param int day in DD format
  173.      * @param int month in MM format
  174.      * @param int year in CCYY format, default to current local year
  175.      *
  176.      * @access public
  177.      *
  178.      * @return int unix timestamp of date
  179.      */
  180.     function HumanToGregorian($day, $month, $year=0)
  181.     {
  182.         /*
  183.          * Check to see if the year has been passed through.
  184.          * If not get current year
  185.          */
  186.         if (empty($year)) {
  187.             $dateinfo = getdate(time());
  188.             $year = $dateinfo["year"];
  189.         }
  190.         /*
  191.          * We need to get the day of the year that we are currently at so that
  192.          * we can work out the Gregorian Month and day
  193.          */
  194.         $DayOfYear = $month * 28;
  195.         $DayOfYear += $day;
  196.         /*
  197.          * Human Calendar starts at 0, so we need to increment $DayOfYear
  198.          * to take into account the day 00
  199.          */
  200.         $DayOfYear++;
  201.         /*
  202.          * the mktime() function will correctly calculate the date for out of
  203.          * range values, so putting $DayOfYear instead of the day of the month
  204.          * will work fine.
  205.          */
  206.         $GregorianTimeStamp = mktime(0, 0, 0, 1, $DayOfYear, $year);
  207.         return $GregorianTimeStamp;
  208.     }
  209. }
  210.